﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BenefitHelper.View
{
    /// <summary>
    /// 交易员年均值
    /// </summary>
    public class OperatorYearAvg
    {
        public int Id { get; set; }

        /// <summary>
        /// 用户编号
        /// </summary>
        public int OperatorId { get; set; }

        public virtual Sys.Operator Operator { get; set; }

        /// <summary>
        /// 交易年
        /// </summary>
        public int Year { get; set; }

        /// <summary>
        /// 平仓盈亏
        /// </summary>
        public double Profit { get; set; }
        /// <summary>
        /// 手续费
        /// </summary>
        public double Free { get; set; }
        /// <summary>
        /// 净收益
        /// </summary>
        public double DayCount { get; set; }
        /// <summary>
        /// 交易笔数
        /// </summary>
        public double BillCount { get; set; }
        /// <summary>
        /// 总亏损
        /// </summary>
        public double SumLost { get; set; }
        /// <summary>
        /// 总盈利
        /// </summary>
        public double SumWin { get; set; }
        /// <summary>
        /// 保证金占用
        /// </summary>
        public double UsedMargin { get; set; }

        /// <summary>
        /// 资金利用率
        /// </summary>
        public double Utilization { get; set; }
        /// <summary>
        /// 胜率
        /// </summary>
        public double WinRate { get; set; }
        /// <summary>
        /// 涨幅
        /// </summary>
        public double RaiseRate { get; set; }

        /// <summary>
        /// 每笔盈利
        /// </summary>
        public double AvgBillBenefit { get; set; }

        /// <summary>
        /// 超过止损次数
        /// </summary>
        public double OverStopLoss { get; set; }

        /// <summary>
        /// 风险次数
        /// </summary>
        public int RiskCount { get; set; }

        /// <summary>
        /// 收益率
        /// </summary>
        public double Yield { get; set; }
        /// <summary>
        /// 最大回撤
        /// </summary>
        public double MaxReturn { get; set; }
        /// <summary>
        /// 平均持仓时间
        /// </summary>
        public double AvgPostionTime { get; set; }
        /// <summary>
        /// 平进平出单量
        /// </summary>
        public int PingJinPing { get; set; }
        /// <summary>
        /// 盈利次数
        /// </summary>
        public int WinCount { get; set; }
        /// <summary>
        /// 亏损次数
        /// </summary>
        public int LoseCount { get; set; }

        public void InitOperatorYearAvg(int tradeHistoryId, DB.DBManager db)
        {
            List<BenefitHelper.Sys.Operator> operators = db.Operators.ToList();
            foreach (BenefitHelper.Sys.Operator op in operators)
            {
                SetOperatorYearAvg(op.Id, tradeHistoryId, db);
            }
            db.SaveChanges();
        }

        public void Delete(int year, DB.DBManager db)
        {
            var query = db.OperatorYearAvg.Where(a => a.Year == year).ToList();
            foreach (OperatorYearAvg s in query)
            {
                db.OperatorYearAvg.Remove(s);
            }
            db.SaveChanges();
        }
        public OperatorYearAvg GetOperatorYearAvg(int operatorid, int year, DB.DBManager db)
        {
            var query = db.OperatorYearAvg.Where(a => a.OperatorId == operatorid).Where(a => a.Year == year).ToList();
            if (query.Count() > 0)
            {
                return query.First();
            }
            else
            {
                return null;
            }

        }
        /// <summary>
        /// 设置交易员年均值
        /// </summary>
        /// <param name="operatorid"></param>
        /// <param name="week"></param>
        /// <param name="month"></param>
        /// <param name="year"></param>
        /// <returns></returns>
        public void SetOperatorYearAvg(int operatorid, int tradeHistoryId, DB.DBManager db)
        {
            OperatorYearAvg change = new OperatorYearAvg();
            BenefitHelper.Sys.TradeHistory th = db.TradeHistorys.Find(tradeHistoryId);
            change.Year = th.YPdate;
            change.OperatorId = operatorid;
            var queryExist = GetOperatorYearAvg(operatorid, th.YPdate, db);
            List<BenefitHelper.Sys.TradeHistory> historys = new BenefitHelper.Sys.TradeHistory().GetMonthTradeingDay(th.MPdate, th.YPdate, db);
            //List<DateTime> tradingdays = new List<DateTime>();
            //foreach (BenefitHelper.Sys.TradeHistory h in historys)
            //{
            //    tradingdays.Add(Convert.ToDateTime(h.YPdate + "-" + h.MPdate + "-" + h.DPdate));
            //}
            int allcount = 0;
            foreach (BenefitHelper.Sys.TradeHistory date in historys)
            {
                List<Sys.Account> accounts = new Sys.DayOperatorAccount().GetDayOperatorAccounts(th.GetDateTimeFromTradeHistoryId(tradeHistoryId, db), operatorid, db);
                if (accounts.Count > 0)
                {
                    foreach (Sys.Account account in accounts)
                    {
                        var query = from t in db.AccountMonthChange where t.AccountId == account.Id && t.Month == date.MPdate && t.Year == date.YPdate select t;
                        if (query.Count() > 0)
                        {
                            allcount++;
                            change.BillCount += query.First().BillCount;
                            change.DayCount += query.First().DayCount;
                            change.Free += query.First().Free;
                            change.LoseCount += query.First().LoseCount;
                            change.OperatorId = operatorid;
                            change.Profit += query.First().Profit;
                            change.SumLost += query.First().SumLost;
                            change.SumWin += query.First().SumWin;
                            change.UsedMargin += query.First().UsedMargin;
                            change.WinCount += query.First().WinCount;
                        }


                        var _query = from t in db.AccountMonthAnalysis where t.AccountId == account.Id && t.Month == date.MPdate && t.Year == date.YPdate select t;
                        if (query.Count() > 0)
                        {
                            change.AvgBillBenefit += _query.First().AvgBillBenefit;
                            change.AvgPostionTime += _query.First().AvgPostionTime;
                            change.MaxReturn += _query.First().MaxReturn;
                            change.OverStopLoss += _query.First().OverStopLoss;
                            change.PingJinPing += _query.First().PingJinPing;
                            change.RaiseRate += _query.First().RaiseRate;
                            change.RiskCount += _query.First().RiskCount;
                            change.Utilization += _query.First().Utilization;
                            change.WinRate += _query.First().WinRate;
                            change.Yield += _query.First().Yield;
                        }
                    }
                }
            }
            if (allcount == 0)
            {
                allcount = 1;
            }
            if (queryExist != null)
            {
                queryExist.AvgBillBenefit = change.AvgBillBenefit / allcount;
                queryExist.AvgPostionTime = change.AvgPostionTime / allcount;
                queryExist.BillCount = change.BillCount / allcount;
                queryExist.DayCount = change.DayCount / allcount;
                queryExist.Free = change.Free / allcount;
                queryExist.LoseCount = change.LoseCount / allcount;
                queryExist.MaxReturn = change.MaxReturn / allcount;
                queryExist.OverStopLoss = change.OverStopLoss / allcount;
                queryExist.PingJinPing = change.PingJinPing / allcount;
                queryExist.Profit = change.Profit / allcount;
                queryExist.RaiseRate = change.RaiseRate / allcount;
                queryExist.RiskCount = change.RiskCount / allcount;
                queryExist.SumLost = change.SumLost / allcount;
                queryExist.SumWin = change.SumWin / allcount;
                queryExist.UsedMargin = change.UsedMargin / allcount;
                queryExist.Utilization = change.Utilization / allcount;
                queryExist.WinCount = change.WinCount / allcount;
                queryExist.WinRate = change.WinRate / allcount;
                queryExist.Yield = change.Yield / allcount;
            }
            else
            {
                db.OperatorYearAvg.Add(change);
            }
        }
    }
}
